package com.example.springboot.utils;

import com.example.springboot.pojo.EmailProperties;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;

import java.util.Properties;

public class MailUtil {
    /**
     * @param emailProperties
     * @param to
     * @param title
     * @param content
     * @return
     * */
    public static boolean sendMail(EmailProperties emailProperties, String to, String title, String content){
        // 设置邮件服务器的属性
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.user", "779907039@qq.com");
        props.put("mail.smtp.code", "yxwxqrmpwndhbdai");


        // 创建邮件会话
        Session session = Session.getInstance(props, null);

        try {
            // 创建MimeMessage对象
            Message message = new MimeMessage(session);

            // 设置发件人和收件人
            message.setFrom(new InternetAddress("779907039@qq.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

            // 设置邮件主题和内容
            message.setSubject(title);
            message.setText(content);

            // 发送邮件
            Transport.send(message);

            System.out.println("邮件发送成功");
        } catch (MessagingException e) {
            System.out.println("邮件发送失败：" + e.getMessage());
        }
        return true;
    }
}
